#include "SyncObject.h"
#include "stdarg.h"
#include "stdio.h"
#include "QString"
#include <QDebug>

SyncObject::SyncObject(QString name, QString type, QString subtype)
{
    initialized = false;
    this->name = name;
    this->type = type;
    this->subtype = subtype;
}
SyncObject::~SyncObject() {
    try {
        //qDebug() << "Removing SyncObject:" << name;
        QListIterator<QVariant*> i(args);
        while (i.hasNext()) {
            QVariant* arg = i.next();
            delete arg;
        }
        args.clear();
    } catch(...) {
        qCritical() << "Error attempting to deallocate resources in a syncObject.";
    }
}
QString SyncObject::getName() {
    return this->name;
}

QString SyncObject::getType() {
    return this->type;
}
QString SyncObject::getSubtype() {
    return this->subtype;
}
void SyncObject::addArgument(QVariant* arg) {
    args.append(arg);
    //emit argAdded(name,args.size(),args.value(arg));
}
void SyncObject::setArgument(int argNum,QVariant* arg) {
    try {
        if(arg->type() == QVariant::Int) {
            args.value(argNum)->setValue(arg->toInt());
        } else if(arg->type() == QVariant::Double) {
            args.value(argNum)->setValue((float)arg->toDouble());
        } else if(arg->type() == QVariant::String) {
           args.value(argNum)->setValue(arg->toString());
        } else {
          args.value(argNum)->setValue((float)arg->toDouble());
        }
        emit argChanged(name, argNum,args.at(argNum));
    } catch(...) {
        qCritical() << "Error attempting to set a syncArg in SyncObject";
    }
}

QVariant* SyncObject::getArgument(int argNum) {
    return args.value(argNum);
}
QList<QVariant*> SyncObject::getArgs() {
    return this->args;
}

void SyncObject::finishInitializtion() {
    initialized = true;
    qDebug() << "Added SyncObject: " << name << "with" << args.size() << "arguments.";
    emit finalized(this);
}

bool SyncObject::getInitialized()
{
    return initialized;
}

